var _signinUtil = require("./signin-util"), _config = _interopRequireDefault(require("../../../utils/config")), _reportAnalytics = require("../../utils/reportAnalytics"), _api = _interopRequireDefault(require("../../utils/api")), _util = _interopRequireDefault(require("../../utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var appData = getApp().globalData;

Component({
    properties: {
        mode: {
            type: String,
            value: "bottom"
        },
        isShown: {
            type: Boolean,
            value: !1,
            observer: function(t) {
                t ? this.show() : this.setData({
                    shown: t
                });
            }
        },
        reload: {
            type: Boolean,
            value: !1
        },
        content: {
            type: String,
            value: "是否登录并继续使用该小程序"
        }
    },
    data: {
        shown: !1,
        appName: _config.default.appName,
        isReject: !1,
        showCancel: !1
    },
    attached: function() {
        this.formIds = [];
    },
    methods: {
        reportAnalytics: function(t) {
            var e = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : _reportAnalytics.ANALYTICS[t];
            if (console.info("report:event:".concat(t, ":").concat(JSON.stringify(e))), !appData.isDevTools) return wx.reportAnalytics(t, e), 
            this.reportAnalyticsInMinicard(t, e);
        },
        reportAnalyticsInMinicard: function(t, e) {
            return _api.default.minicard.common.createeventtracking({
                data: {
                    key: t,
                    params: e
                }
            });
        },
        _onSubmit: function(t) {
            this.formIds.push(t.detail.formId);
        },
        _signIn: function(t) {
            var e = this;
            this.reportAnalytics("authorize_modal_click"), (0, _signinUtil.registerToken)(t).then(this._success.bind(this)).catch(function() {
                e.setData({
                    shown: !0,
                    isReject: !0
                }), e.reportAnalytics("authorize_fail");
            });
        },
        _success: function() {
            this.reportAnalytics("authorize_success"), this.reportFormIds(), wx.showToast({
                title: "登录成功",
                duration: 500
            }), this.data.reload ? this.reLoad() : (this.hide(), this.triggerEvent("success", {}, {}));
        },
        reportFormIds: function() {
            this.formIds.reduce(function(t, e) {
                return t.then(function() {
                    return _api.default.weapp.collectformid({
                        data: {
                            formid: e,
                            app: _config.default.WX_APP_NAME
                        }
                    });
                });
            }, Promise.resolve());
        },
        _onClose: function() {
            this.setData({
                shown: !1
            });
        },
        show: function() {
            this.reportAnalytics("authorize_modal_agree"), this.setData({
                shown: !0
            });
        },
        hide: function() {
            this._onClose();
        },
        reLoad: function() {
            var t = getCurrentPages(), e = t[t.length - 1], i = "/".concat(e.route, "?").concat(_util.default.serialize(e.__query));
            1 === t.length && wx.reLaunch ? wx.reLaunch({
                url: i
            }) : wx.redirectTo({
                url: i
            });
        }
    }
});